package com.ruoyi.hospital.patient.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hospital.patient.domain.TPatient;
import com.ruoyi.hospital.patient.service.ITPatientService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 患者管理Controller
 *
 * @author Shawn
 * @date 2024-02-21
 */
@Controller
@RequestMapping("/hospital/patient")
public class TPatientController extends BaseController {
    private String prefix = "hospital/patient";

    @Autowired
    private ITPatientService tPatientService;

    @RequiresPermissions("hospital:patient:view")
    @GetMapping()
    public String patient() {
        return prefix + "/patient";
    }

    /**
     * 查询患者管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TPatient tPatient) {
        startPage();
        List<TPatient> list = tPatientService.selectTPatientList(tPatient);
        return getDataTable(list);
    }

    /**
     * 导出患者管理列表
     */
    @RequiresPermissions("hospital:patient:export")
    @Log(title = "患者管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TPatient tPatient) {
        List<TPatient> list = tPatientService.selectTPatientList(tPatient);
        ExcelUtil<TPatient> util = new ExcelUtil<TPatient>(TPatient.class);
        return util.exportExcel(list, "患者管理数据");
    }

    /**
     * 新增患者管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存患者管理
     */
    @RequiresPermissions("hospital:patient:add")
    @Log(title = "患者管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TPatient tPatient) {
        return toAjax(tPatientService.insertTPatient(tPatient));
    }

    /**
     * 修改患者管理
     */
    @RequiresPermissions("hospital:patient:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TPatient tPatient = tPatientService.selectTPatientById(id);
        mmap.put("tPatient", tPatient);
        return prefix + "/edit";
    }

    /**
     * 修改保存患者管理
     */
    @RequiresPermissions("hospital:patient:edit")
    @Log(title = "患者管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TPatient tPatient) {
        return toAjax(tPatientService.updateTPatient(tPatient));
    }

    /**
     * 删除患者管理
     */
    @RequiresPermissions("hospital:patient:remove")
    @Log(title = "患者管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tPatientService.deleteTPatientByIds(ids));
    }

    @RequestMapping("/selectPatient")
    public String selectPatient() {
        return prefix + "/selectPatient";
    }
}
